//
//  HNSH_GoodsSpecCountFooter.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/25.
//

#import "HNSH_GoodsSpecCountFooter.h"

@interface HNSH_GoodsSpecCountFooter ()
/**  */
@property (nonatomic, strong)UILabel * titleLabel;
/**  */
@property (nonatomic, strong)UIButton * lessBtn;
/**  */
@property (nonatomic, strong)UILabel * countLabel;
/**  */
@property (nonatomic, strong)UIButton * addBtn;
@end

@implementation HNSH_GoodsSpecCountFooter

//MARK: Cycle Methods
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.count = 1;
        [self addAllViews];
        [self addAllLayout];
    }
    return self;
}

- (void)addAllViews{
    [self addSubview:self.titleLabel];
    [self addSubview:self.lessBtn];
    [self addSubview:self.countLabel];
    [self addSubview:self.addBtn];
}

- (void)addAllLayout{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.height.mas_equalTo(self);
    }];
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.width.height.mas_equalTo(22);
        make.centerY.mas_equalTo(self);
    }];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.addBtn.mas_left);
        make.width.mas_equalTo(34);
        make.top.height.mas_equalTo(self.addBtn);
    }];
    [self.lessBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.countLabel.mas_left);
        make.top.width.height.mas_equalTo(self.addBtn);
    }];
}

//MARK: Myself Methods
- (void)lessBtnAction{
    if (self.count <= 1) {
        return;
    }
    self.count--;
    self.countLabel.text = [NSString stringWithFormat:@"%ld", self.count];
}

- (void)addBtnAction{
    if ([self.goodsType isEqualToString:@"5"] && self.count >= 1) {
        [kSTWindow makeToast:@"新人商品只能购买一个哦"];
        self.count = 1;
        self.countLabel.text = [NSString stringWithFormat:@"%ld", self.count];
        return;
    }
    if (self.count >= self.maxCount) {
        return;
    }
    self.count++;
    self.countLabel.text = [NSString stringWithFormat:@"%ld", self.count];
}

//MARK: Getter Methods
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textColor = [UIColor appBlack333LabelColor];
        _titleLabel.text = @"数量";
    }
    return _titleLabel;
}

- (UIButton *)lessBtn{
    if (!_lessBtn) {
        _lessBtn = [UIButton new];
        [_lessBtn setImage:[UIImage imageNamed:@"goods_detail_count_jian"] forState:UIControlStateNormal];
        [_lessBtn addTarget:self action:@selector(lessBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _lessBtn;
}

- (UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [UIButton new];
        [_addBtn setImage:[UIImage imageNamed:@"goods_detail_count_add"] forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(addBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [UILabel new];
        _countLabel.textAlignment = NSTextAlignmentCenter;
        _countLabel.font = [UIFont systemFontOfSize:14];
        _countLabel.textColor = [UIColor appBlack333LabelColor];
        _countLabel.text = [NSString stringWithFormat:@"%ld", self.count];
    }
    return _countLabel;
}
@end
